<script>
    var __AddUrl   = '/cms/site/add.mhtml?parentId=';
    var __EditUrl   = '/cms/site/edit.mhtml?resId=';
    var __DelUrl    = '/cms/site/del.do';
    var __ListUrl  = '/cms/site/list.do';
    function delData (resId){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: __DelUrl,
            data: {resId:resId},
            success: function (res) {
                layer.close(loadIndex);
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        window.location.reload()
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            }
        });
    }
    let EleTree ;
    function openAddPanel(pid){
        let index = layer.open({
            title: '添加站点',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __AddUrl+pid,
        });
    }

    function openEditPanel(resId){
        let index = layer.open({
            title: '编辑部门信息',
            type: 2,
            shade: 0.2,
            maxmin:true,
            shadeClose: true,
            area: ['100%', '100%'],
            content: __EditUrl+resId,
        });
    }

    function initData (eleTree,data){

        let obj={
            elem: '.eleTree',
            data: data,
            showLine:true,
            // url: "../eleTree/tree.json",
            renderAfterExpand: true,
            expandOnClickNode: true,
            contextmenuList:[{eventName: "editSite", text: "编辑站点"},{eventName: "delSite", text: "删除站点"}]
        }
        $("#btn-add").click(function(e){
            e.preventDefault();
            openAddPanel(-1);
        });

        //删除部门
        eleTree.on("nodeDelSite",function(d) {

            layer.confirm("确认删除该记录？",{
                title:"操作确认",
                yes:function(index,layero){
                    delData(d.data.id)
                    layer.close(index)
                },
                cancel:function(index){
                }

            })
        })
        //编辑部门信息
        eleTree.on("nodeEditSite",function(d) {
            console.log(d);
            openEditPanel(d.data.id)
        })
        EleTree = eleTree.render(obj);
    }
    layui.use(['eleTree'], function () {
        let eleTree = layui.eleTree;

        $.ajax({
            type: "get",
            url: __ListUrl,
            success: function (res) {

                if(res.code==0){
                    let treeData = [] ;
                    if(res.data){
                        treeData = listToTree("id","parentId",res.data)
                    }
                    initData(eleTree,treeData)
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(err){
                layer.alert(err)
            },
            complete:function (){
                layer.closeAll();
            }
        });

        //el1.append(d,newData)

    });

</script>
